from solana.rpc.websocket_api import connect
from solders.pubkey import Pubkey
import asyncio

async def subscribe_raydium():
    RAYDIUM_AMM = Pubkey.from_string('675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8')
    RAYDIUM_CLMM = Pubkey.from_string('CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK')
    async with connect('wss://multi-blue-brook.solana-mainnet.quiknode.pro/bd0f89f51485a96a47c8040bb77368dbd7e0c03c/') as ws:
        subscription_id = await ws.logs_subscribe(
            commitment='confirmed',
            filters=[{'mentions': [RAYDIUM_AMM, RAYDIUM_CLMM]}]
        )
        async for msg in ws:
            if 'initialize' in msg.result.value.logs:
                print(f"Raydium 池子创建: {msg.result.value.signature}")
                # 可解析日志获取池子地址
                logs = msg.result.value.logs
                for log in logs:
                    if 'Pool' in log:
                        print(f"  池子地址（示例）: {log}")
        await ws.logs_unsubscribe(subscription_id)

async def main():
    await subscribe_raydium()

if __name__ == "__main__":
    asyncio.run(main())